package com.example.controller;

import com.example.client.OrderClient;
import com.example.entity.TOrder;
import com.example.entity.User;
import com.example.service.IUserService;
import com.hzit.util.R;
import org.bouncycastle.pqc.crypto.newhope.NHOtherInfoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Qualifier("com.example.client.OrderClient")
    @Autowired
    private OrderClient orderClient;

    @GetMapping("/list")
    public R list(){
        return R.ok(userService.list());
    }
    // 登录接口

    @PostMapping("/login")
    public R login(@RequestBody User user) {
        User loggedInUser = userService.login(user.getAccount(), user.getPassword());
        if (loggedInUser != null) {
            return R.ok("登录成功");
        } else {
            return R.fail("用户名或密码错误");
        }
    }
    @PostMapping("/add")
    public R add(@RequestBody User user){
        boolean flag = userService.save(user);
        return flag ? R.ok("添加成功") : R.fail("添加失败");
    }
    @PutMapping("/update")
    public R update(@RequestBody User user){
        boolean flag = userService.updateById(user);
        return flag ? R.ok("修改成功") : R.fail("修改失败");
    }
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        boolean flag = userService.removeById(id);
        return flag ? R.ok("删除成功") : R.fail("删除失败");
    }
    @GetMapping("/tOrder/list")
    public R<List<TOrder>> orderList(){
        return orderClient.list();
    }

    @PostMapping("/tOrder/add")
    public R addOrder(@RequestBody TOrder order){
        return orderClient.add(order);
    }
    @PutMapping("/tOrder/update")
    public R updateOrder(@RequestBody TOrder order){
        return orderClient.update(order);
    };

    @DeleteMapping("/tOrder/delete/{id}")
    public R deleteOrder(@PathVariable Integer id){
        return orderClient.delete(id);
    };
}
